package com.app.community.controller;

import com.app.community.exception.ErrorCodeEnum;
import com.app.community.service.EmailSendService;
import com.app.community.service.RegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户注册页面
 * @Date 2020/10/14 20:28
 **/
@Slf4j
@Controller
public class RegisterController {

    @Autowired
    RegisterService registerService;

    @Autowired
    EmailSendService emailSendService;
    // 验证码
    Map<String,Integer> verificationCode  = new HashMap<>();

    @GetMapping("register")
    public String register(){
        return "common/register";
    }

    @PostMapping("register")
    public String register(@RequestParam(value = "userId",required = false)String userId, @RequestParam(value = "password",required = false) String password,@RequestParam(value = "passwordRepeat",required = false) String passwordRepeat,@RequestParam(value = "verificationCode",required = false)Integer verificationCode, Model model){
        model.addAttribute("userId",userId);
        model.addAttribute("password",password);

        if (userId == null || userId.isEmpty()){
            model.addAttribute("error",ErrorCodeEnum.INVALIDEMAIL.getDescription());
            return "common/register";
        }

        if (registerService.isUserExist(userId)){
            model.addAttribute("error",ErrorCodeEnum.HAVEREGISTER.getDescription());
            return "common/register";
        }

        if (password == null || password.isEmpty()){
            model.addAttribute("error",ErrorCodeEnum.INITIALPASSWORDERROR.getDescription());
            return "common/register";
        }

        if (passwordRepeat == null || passwordRepeat.isEmpty()){
            model.addAttribute("error",ErrorCodeEnum.HAVENOTCONFIRMPASSWORD.getDescription());
            return "common/register";
        }

        if (!password.equals(passwordRepeat)){
            model.addAttribute("error",ErrorCodeEnum.PASSWORDNOTMATCH.getDescription());
            return "common/register";
        }

        if (verificationCode == null){
            model.addAttribute("error",ErrorCodeEnum.EMPTYVERTIFICATIONCODE.getDescription());
            return "common/register";
        }

        if (!verificationCode.equals(this.verificationCode.get(userId))){
            model.addAttribute("error",ErrorCodeEnum.ERRORVERTIFICATIONCODE.getDescription());
            return "common/register";
        }

        registerService.registerUser(userId,password);
        log.info("新用户:" + userId + "注册成功！");
        return "redirect:login";
    }

    /**
     * 向用户的邮箱发送验证码，同时不能刷新界面，所以设置void方法。
     */
    @PostMapping("registerEmail")
    public void registerEmail(@RequestParam("emailAddress")String email){
        verificationCode.put(email,emailSendService.sendRegisterEmail(email));
    }

}
